package com.nulldev.util.web.HttpServer3.extensions.nCaptcha.captchas.advanced;

import java.awt.image.BufferedImage;

import com.nulldev.util.VariableAPI.RandomUtil;
import com.nulldev.util.VariableAPI.StringsUtil;
import com.nulldev.util.data.Images.ImageUtil;
import com.nulldev.util.data.Images.legacy.TextObf;
import com.nulldev.util.data.Images.legacy.TextObf.ObfuscationMethod;
import com.nulldev.util.data.Sets.DataSet;
import com.nulldev.util.web.HttpServer3.extensions.nCaptcha.backend.Captcha;
import com.nulldev.util.web.HttpServer3.pages.v3.HttpPage3.Request;

public class MultiWordCheck implements Captcha {

	@Override
	public DataSet<String, String[]> generateBase64Image(final int botLevel, final Request request) {
		final ObfuscationMethod method;
		if (botLevel > 1) {
			method = ObfuscationMethod.V2_EXPERIMENT_ONE;
		} else {
			method = ObfuscationMethod.RainbowBackgroundPixels;
		}
		final int word1Len = RandomUtil.randInt(4, 6);
		final int word2Len = RandomUtil.randInt(4, 6);
		final int spaces = RandomUtil.randInt(1, 3);
		final String w1 = StringsUtil.randomString(word1Len);
		final String w2 = StringsUtil.randomString(word2Len);
		final String displayWord = w1 + StringsUtil.repeat(" ", spaces) + w2;
		final String answerWord = w1 + " " + w2;
		final BufferedImage bi = TextObf.obfuscate(displayWord, method, TextObf.__font(BASE_TEXT_SIZE));
		return new DataSet<String, String[]>("data:image/png;base64," + ImageUtil.convertImageDataBase64(bi), new String[]
			{ answerWord });
	}

	@Override
	public String getType() {
		return "multiword";
	}

}
